package com.fantabest.frontend.client.widget;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.fantabest.common.client.AppEvents;
import com.fantabest.common.client.FantaBestConstants;
import com.fantabest.common.client.FantaBestMessages;
import com.fantabest.common.client.LoginServiceAsync;
import com.fantabest.common.client.RegistryKeys;
import com.fantabest.common.entity.Manager;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginWindow extends Window {

	private FormPanel loginPanel;
	private TextField<String> name;
	private TextField<String> surname;
	private TextField<String> nickname;
	private TextField<String> email;
	private TextField<String> password;
	private TextField<String> retypePassword;
	
	private Button submit;
	private Button signin;
	private Status status;
	private FormButtonBinding binding;
	
	FantaBestConstants constants = Registry.get(RegistryKeys.CONSTANTS);
	FantaBestMessages messages = Registry.get(RegistryKeys.MESSAGES);
	
	public LoginWindow() {
		super();
		setModal(true);
		setDraggable(false);
		setHeading("Login");
		add(createLoginPanel());
		setResizable(false);
	}

	private FormPanel createLoginPanel()	{
		loginPanel = new FormPanel();
		loginPanel.setId("login");
		loginPanel.setStyleName("login-style");
		loginPanel.setFrame(true);
		loginPanel.setCollapsible(false);
		loginPanel.setHeaderVisible(false);
		loginPanel.setLabelAlign(LabelAlign.RIGHT);
		loginPanel.setLabelWidth(90);
		loginPanel.setFieldWidth(160);
		loginPanel.setPadding(0);
		loginPanel.setButtonAlign(HorizontalAlignment.CENTER);
		
		name = new TextField<String>();
		name.setFieldLabel(constants.name());
		name.setAllowBlank(false);
		name.setMaxLength(30);
		name.getMessages().setBlankText(messages.required(constants.name()));
		
		
		surname = new TextField<String>();
		surname.setAllowBlank(false);
		surname.setFieldLabel(constants.surname());
		surname.setMaxLength(30);
		surname.getMessages().setBlankText(messages.required(constants.surname()));		
		
		nickname = new TextField<String>();
		nickname.setFieldLabel(constants.nickname());
		nickname.setAllowBlank(false);
		nickname.setMaxLength(30);
		nickname.getMessages().setBlankText(messages.required(constants.nickname()));
		
		email = new TextField<String>();
		email.setFieldLabel(constants.email());
		email.setAllowBlank(false);
		email.setAutoValidate(true);
		email.setRegex(".+@.+\\.[a-z]+");
		email.getMessages().setRegexText("Invalid email.");
		email.setMaxLength(30);
		email.getMessages().setBlankText(messages.required(constants.email()));
		
		password = new TextField<String>();
		password.setFieldLabel(constants.password());
		password.setAllowBlank(false);
		password.setAutoValidate(true);
		password.setPassword(true);
		password.setMaxLength(30);
		password.getMessages().setBlankText(messages.required(constants.password()));
		
		retypePassword = new TextField<String>();
		retypePassword.setFieldLabel("Retype password");
		retypePassword.setAllowBlank(false);
		retypePassword.setPassword(true);
		retypePassword.setMaxLength(30);
		retypePassword.getMessages().setBlankText(messages.required(constants.name()));
		
		
		signin = new Button("Signin");
		signin.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				LoginServiceAsync loginService = Registry.get(RegistryKeys.LOGIN_SERVICE);
				loginService.login(email.getValue(), password.getValue(), new AsyncCallback<Manager>() {
					public void onSuccess(Manager result) {
						BeanModel beanModel = null;
						if(result!=null)	{
							BeanModelFactory factory = BeanModelLookup.get().getFactory(Manager.class);
							beanModel = factory.createModel(result);
						}
						Dispatcher.get().dispatch(AppEvents.Login, beanModel);
					}
					public void onFailure(Throwable caught) {
						MessageBox.alert("Errore", caught.getMessage(), null);
					}
				});
			}
		});
		
		submit = new Button("Submit");
		submit.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				LoginServiceAsync loginService = Registry.get(RegistryKeys.LOGIN_SERVICE);
				loginService.registerUser(name.getValue(), surname.getValue(), nickname.getValue()
						, email.getValue(), password.getValue(), new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						setStatusMessage("Salvataggio fallito");
					}
					public void onSuccess(String result) {
						clear();
					}
				});
			}
		});	
		submit.setVisible(false);
		
		
		final Button register = new Button("Register");
		register.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				binding.removeButton(signin);
				binding.addButton(submit);
				
				name.setVisible(true);
				surname.setVisible(true);
				nickname.setVisible(true);
				retypePassword.setVisible(true);
				register.setVisible(false);
				signin.setVisible(false);
				submit.setVisible(true);
				
				status.clearStatus("");
			}
		});
		register.setText("Register");
		
		status = new Status();
		status.setStyleAttribute("color", "red");
		
		loginPanel.add(name);
		loginPanel.add(surname);
		loginPanel.add(nickname);
		loginPanel.add(email);
		loginPanel.add(password);
		loginPanel.add(retypePassword);
		loginPanel.addButton(signin);
		loginPanel.addButton(register);
		loginPanel.addButton(submit);
		loginPanel.getButtonBar().add(status);
		
		
		binding = new FormButtonBinding(loginPanel);  

		clear();
		
		return loginPanel;
	}
	
	public void setStatusMessage(String message)	{
		status.setText(message);
	}
	
	public void clear()	{
		binding.removeButton(submit);
		binding.addButton(signin);
		
		submit.setVisible(false);
		name.setVisible(false);
		surname.setVisible(false);
		nickname.setVisible(false);
		retypePassword.setVisible(false);
		
		email.setValue("");
		password.setValue("");
		
		status.clearStatus("");
	}
	
	
	@Override
	protected void afterShow() {
		name.setValue(constants.name());
		surname.setValue(constants.surname());
		nickname.setValue(constants.nickname());
		email.setValue("email@gmail.com");
		password.setValue(constants.password());
		retypePassword.setValue("Retype password");
		super.afterShow();
	}
	
	
}
